﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace EasyMapping.Core
{
    public class DataProviderFactory
    {
        private static IDataProvider _cachedProvider;
        private static volatile object SynObject = new object();

        public static IDataProvider GetDataProvider()
        {
            if (_cachedProvider == null)
            {
                lock (SynObject)
                {
                    foreach (var item in typeof(IDataProvider).GetCustomAttributes(false))
                    {
                        if (item is DefaultImplementationClassAttribute)
                        {
                            DefaultImplementationClassAttribute temp = item as DefaultImplementationClassAttribute;
                            if (!string.IsNullOrEmpty(temp.ClassName))
                            {
                                Type type = Type.GetType("EasyMapping.Core." + temp.ClassName);
                                _cachedProvider = (IDataProvider)ReflectionManager.GetInstance(type);
                                return _cachedProvider;
                            }
                        }
                    }              
                }
            }
            return _cachedProvider;  
        }
    }
}
